//
//  LRNavBaseViewController.m
//  本地生活圈
//
//  Created by admin on 15/8/10.
//  Copyright (c) 2015年 懒人. All rights reserved.
//

#import "LRNavBaseViewController.h"
#import "LRLoginViewController.h"
#import "LRPersonalViewController.h"

@interface LRNavBaseViewController ()



@end

@implementation LRNavBaseViewController

- (AFHTTPRequestOperationManager *)manager
{
    if (!_manager)
    {
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Nav
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenW, kNavHeight)];
    
    self.navView.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:self.navView];
    
    // 回退
    self.leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, kNavStatusHeight, kNavButtonWidth, kNavButtonHeight)];
    
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    
    [self.leftButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navView addSubview:self.leftButton];
    
    
    
    // 标题
    self.titleLabel = [[UILabel alloc]init];
    
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    
    self.titleLabel.textColor = [UIColor whiteColor];
    
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel.text = @"本地生活圈";
    
    [self.navView addSubview:self.titleLabel];
    
    self.rightButton = [[UIButton alloc]init];
    [self.navView addSubview:self.rightButton];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    CGRect rect = [self.titleLabel.text boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil];
    
    self.titleLabel.frame = CGRectMake((kUIScreenW - rect.size.width)/2,kNavStatusHeight +(kNavButtonHeight - rect.size.height)/2, rect.size.width, rect.size.height);
    
    // 是否已经登录状态
    if (![LRUserInfo shareInstance].isLogin)
    {
        [self.rightButton setImage:nil forState:UIControlStateNormal];
        [self.rightButton setTitle:@"登录/注册" forState:UIControlStateNormal];
        [self.rightButton removeTarget:self action:@selector(personInfo) forControlEvents:UIControlEventTouchUpInside];
        [self.rightButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
        self.rightButton.titleLabel.textColor = [UIColor whiteColor];
        self.rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.rightButton.frame = CGRectMake(kUIScreenW - 2 * kNavButtonWidth, kNavStatusHeight, 2*kNavButtonWidth, kNavButtonHeight);
    }
    else
    {
        // 登录
        [self.rightButton setTitle:nil forState:UIControlStateNormal];
        [self.rightButton sd_setImageWithURL:[NSURL URLWithString:[LRUserInfo shareInstance].small_avatar] forState:UIControlStateNormal];
        self.rightButton.frame = CGRectMake(kUIScreenW - kNavButtonWidth, kNavStatusHeight, kNavButtonWidth - 10, kNavButtonHeight - 10);
        self.rightButton.layer.cornerRadius = self.rightButton.bounds.size.width / 2.0;
        self.rightButton.layer.masksToBounds = YES;
        [self.rightButton removeTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
        [self.rightButton addTarget:self action:@selector(personInfo) forControlEvents:UIControlEventTouchUpInside];
    }
    
   
}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
    // 显示tabbar
    UIApplication *app = [UIApplication sharedApplication];
    LRTabBarViewController *tabBar = (LRTabBarViewController *)app.keyWindow.rootViewController;
    tabBar.tabBarView.hidden = NO;
}

- (void)loginClick
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    LRLoginViewController *loginVc = [sb instantiateViewControllerWithIdentifier:@"LRLoginViewController"];
    [self.navigationController pushViewController:loginVc animated:YES];
}

- (void)personInfo
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LRPersonalViewController *personalVc = [sb instantiateViewControllerWithIdentifier:@"LRPersonalViewController"];
    personalVc.titleLabel.text = @"个人中心";
    [self.navigationController pushViewController:personalVc animated:YES];
}
@end
